﻿using SoTaySinhVienUIT.Data;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Graphics.Display;
using Windows.UI.ApplicationSettings;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Items Page t template is documented at http://go.microsoft.com/fwlink/?LinkId=234233

namespace SoTaySinhVienUIT {
    /// <summary>
    /// A page that displays a collection of t previews.  In the Split Application this page
    /// is used to display and select one of the available stsvs.
    /// </summary>
    public sealed partial class ItemsPage : SoTaySinhVienUIT.Common.LayoutAwarePage {
        public ItemsPage() {
            this.InitializeComponent();

        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState) {

            var topic = SampleDataSource.GetItem((String)navigationParameter);

            // TODO: Create an appropriate data model for your problem domain to replace the sample data
            var sampleDataGroups = topic.Items;
            this.DefaultViewModel["Title"] = topic.Title;
            this.DefaultViewModel["Items"] = sampleDataGroups;
            this.DefaultViewModel["Topic"] = topic;

            SettingsPane.GetForCurrentView().CommandsRequested += ItemsPagePrivacy_CommandsRequested;
        }

        void ItemsPagePrivacy_CommandsRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args) {
            SampleDataSource.AddSettingsCommands(args);
        }

        /// <summary>
        /// Invoked when an t is clicked.
        /// </summary>
        /// <param name="sender">The GridView (or ListView when the application is snapped)
        /// displaying the t clicked.</param>
        /// <param name="e">Event data that describes the t clicked.</param>
        void ItemView_ItemClick(object sender, ItemClickEventArgs e) {
            // Navigate to the appropriate destination page, configuring the new page
            // by passing required information as a navigation parameter
            var itemId = ((SampleDataItem)e.ClickedItem).UniqueId;
            var group = ((SampleDataItem)e.ClickedItem).Topic;

            var dic = new Dictionary<String, String>(){
                {"topic_id", ((SampleDataTopic)group).UniqueId},
                {"item_id", itemId}
            };

            this.Frame.Navigate(typeof(SplitPage), dic);
            
        }

        private void btnHome_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
        	this.Frame.Navigate(typeof(TopicGroupedPage), "AllGroups");
        }

        private void pageTitle_Click(object sender, RoutedEventArgs e) {
            // Navigate to the appropriate destination page, configuring the new page
            // by passing required information as a navigation parameter
            
            var group = this.DefaultViewModel["Topic"] as SampleDataTopic;
            var itemId = group.Items.ElementAt(0) as SampleDataItem;

            var dic = new Dictionary<String, String>(){
                {"topic_id", ((SampleDataTopic)group).UniqueId},
                {"item_id", itemId.UniqueId}
            };

            this.Frame.Navigate(typeof(SplitPage), dic);
        }
    }
}
